using System;
using System.Globalization;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AB.ServiceDirectory.BO;
using AB.ServiceDirectory.Commons;
using AB.ServiceDirectory.Entities;

namespace AB.ServiceDirectory.Design.SupportingMaterials
{
    public partial class HAddSupportingMaterial : Page
    {
        /// <summary>
        /// Gets or sets the Organization id.
        /// </summary>
        /// <value>
        /// The Organization id.
        /// </value>
        public int OrganizationId
        {
            get
            {
                if (ViewState["OrganizationId"] != null)
                {
                    return (int) ViewState["OrganizationId"];
                }
                return -1;
            }

            set { ViewState["OrganizationId"] = value; }
        }

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDropDownListType();
                BindFields();
            }
        }

        /// <summary>
        /// Buttons the save click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void ButtonSaveClick(object sender, EventArgs e)
        {
            bool result = false;
            SupportingMaterial supportingMaterial = GetSupportingMaterial();
            if (supportingMaterial != null)
            {
                result = HSupportingMaterialBO.InsertSupportingMaterial(supportingMaterial);
                if (result)
                {
                    Response.Redirect(string.Format(CultureInfo.CurrentCulture,
                                                    "~/Organisation/OrganisationDetails.aspx?id={0}#DetailsPanel05",
                                                    OrganizationId));
                    return;
                }
            }

            DialogBox.Error("Cannot insert Supporting Material !");
        }

        /// <summary>
        /// Binds the type of the drop down list.
        /// </summary>
        private void BindDropDownListType()
        {
            DropDownListType.DataSource = DocumentUtilities.SupportingMaterialTypes;
            DropDownListType.DataBind();
            DropDownListType.Items.Insert(0, new ListItem("None", string.Empty));
        }

        /// <summary>
        /// Binds the fields.
        /// </summary>
        private void BindFields()
        {
            int organisationId;
            if (int.TryParse(Request.Params["organisationId"], out organisationId))
            {
                if (OrganizationBO.GetOrganizationById(organisationId) != null)
                {
                    OrganizationId = organisationId;
                    ButtonSave.Enabled = true;
                    ButtonBack.Enabled = true;
                    TextBoxAddedBy.Text = HttpContext.Current.User.Identity.Name;
                    TextBoxAddedDate.Text = DateTime.Now.ToShortDateString();
                }
            }
        }

        /// <summary>
        /// Gets the supporting material from fields in the Page
        /// </summary>
        /// <returns></returns>
        private SupportingMaterial GetSupportingMaterial()
        {
            if (Session["userId"] != null)
            {
                var supportingMaterial = new SupportingMaterial();
                supportingMaterial.Url = TextBoxURL.Text;
                supportingMaterial.Description = TextBoxDescription.Text;
                supportingMaterial.Type = DropDownListType.SelectedValue;
                supportingMaterial.UserId = (int) Session["userId"];
                supportingMaterial.AddedDate = DateTime.Now;
                supportingMaterial.IsActive = true;
                supportingMaterial.OrganizationId = OrganizationId;
                return supportingMaterial;
            }
            return null;
        }

        /// <summary>
        /// Handles the Command event of the ButtonBack control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.CommandEventArgs"/> instance containing the event data.</param>
        protected void ButtonBack_Command(object sender, CommandEventArgs e)
        {
            Response.Redirect(string.Format(CultureInfo.CurrentCulture,
                                            "~/Organisation/OrganisationDetails.aspx?id={0}#DetailsPanel05",
                                            OrganizationId));
        }
    }
}